package com.ikas.ai.server.module.equipment.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.equipment.model.po.EquipmentPosition;
import com.ikas.ai.server.module.equipment.model.vo.EquipmentPositionVo;
import com.ikas.ai.server.module.equipment.service.EquipmentPositionService;
import com.ikas.ai.server.module.system.model.User;
import com.ikas.ai.utils.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备部位
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/2/1 15:56
 */
@RestController
@RequestMapping("/equipment/position")
public class EquipmentPositionController extends AbstractBean {

    @Autowired
    private EquipmentPositionService equipmentPositionService;

    /**
     * 获取设备部位，通过设备编号
     *
     * @return List<EquipmentPosition>
     */
    @GetMapping("/list/byEquipmentCode")
    public JsonResult getEquipmentPositionByEquipmentCode(@RequestParam(value = "equipmentCode") String equipmentCode) {
        return JsonResult.ok(equipmentPositionService.getEquipmentPositionByEquipmentCode(equipmentCode));
    }

    /**
     * 新增设备部位
     *
     * @param param   param
     * @return row
     */
    @PostMapping("/add")
    public JsonResult addEquipmentPosition(@RequestBody EquipmentPosition param) {
        User user = getUser();
        param.setCreator(user.getId());
        return JsonResult.ok(equipmentPositionService.addEquipmentPosition(param));
    }

    /**
     * 新增设备部位（批量）
     *
     * @param param   param
     * @return row
     */
    @PostMapping("/add/batch")
    public JsonResult addEquipmentPositions(@RequestBody @Validated EquipmentPositionVo param) {
        User user = getUser();
        param.setCreator(user.getId());
        return JsonResult.ok(equipmentPositionService.addEquipmentPositions(param));
    }

    /**
     * 删除设备部位
     *
     * @param ids     ids
     * @return row
     */
    @PostMapping("/{ids}")
    public JsonResult deleteEquipmentPosition(@PathVariable(name = "ids") String ids) {
        List<Long> equipmentIds = new ArrayList<>();
        for (String id : ids.split(",")) {
            if (!NumUtil.isLong(id, 0, Long.MAX_VALUE)) {
                continue;
            }
            equipmentIds.add(Long.parseLong(id));
        }
        return JsonResult.ok(equipmentPositionService.deleteEquipmentPosition(equipmentIds));
    }

}
